<?php

class TranslationManager
{
	public static $DefaultLangCode = 'en';
	
	public static function GetText( $key, $langCode )
	{
		if($bean = R::findOne('translation', 'textkey=\'' . $key . '\' and langcode=\'' . $langCode . '\''))
		{
			return $bean->textvalue;
		}
		else 
			return self::GenerateDefaultTranslation($key, $langCode);
	}
	
	public static function GenerateDefaultTranslation($key, $langCode)
	{
		$bean = R::dispense('translation');
		$bean->textkey = $key;
		$bean->langCode = $langCode;
		if($langCode == self::$DefaultLangCode)
			$bean->textvalue = $key;
		else
		{
			$default = self::GetText( $key, self::$DefaultLangCode );				
			$GTranslate = new GTranslate;
			$bean->textvalue = $GTranslate->translate(self::$DefaultLangCode,$langCode,$default);
		}	
		R::store($bean);
		return $bean->textvalue;
	}

	public static function SetText( $key, $langCode, $text )
	{
		if($bean = R::findOne('translation', 'textkey=\'' . $key . '\' and langcode=\'' . $langCode . '\''))
		{
			$bean->textvalue = $text;
			R::store($bean);
		}
		else
		{
			$bean = R::dispense('translation');
			$bean->textkey = $key;
			$bean->langCode = $langCode;
			$bean->textvalue = $text;
			R::store($bean);		
		}
	}
}